using System;
using System.Globalization;
using System.Windows.Data;

namespace LayoutPlanning.Converters
{
    public class MutiplyValue : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var faktor = value as double?;
            var secfactor = parameter as double?;
            double parse = 0;
            if (secfactor.HasValue)
                parse = secfactor.Value;
            if(faktor == null || (secfactor ==null && parameter is string && !double.TryParse(parameter as string, out parse)))
                return 0;
            else
            {
                return faktor.Value*parse;
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var faktor = value as double?;
            var secfactor = parameter as double?;

            if (faktor == null || secfactor == null)
            {
                return 0;
            }
            else
            {
                return faktor.Value / secfactor.Value;
            }
        }
    }
}